<template>
  <el-container class="home_Container">
    <el-header>
      <div>
        <img src="../assets/avatar.jpg" alt="logo" />
        <span>商城后台管理系统</span>
      </div>
      <el-button type="info" size="small" @click="logout">退出登陆</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollApse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <span>|||</span>
        </div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :unique-opened="true"
          :collapse="isCollApse"
          :collapse-transition="false"
          router
          :default-active="activeColor"
        >
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <i :class="iconobj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="'/'+subitem.path"
                v-for="subitem in item.children"
                :key="subitem.id"
                @click="setActiveColor('/'+subitem.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{subitem.authName}}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
</script>
<script>
export default {
  data() {
    return {
      menulist: [],
      iconobj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao"
      },
      isCollApse: false,
      activeColor: '',
    };
  },
  created() {
    this.getMenuList();
    this.activeColor = sessionStorage.getItem('activeColor');
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    async getMenuList() {
      let { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    toggleCollapse() {
      this.isCollApse = !this.isCollApse;
    },
    setActiveColor(data){
      window.sessionStorage.setItem('activeColor',data);
      this.activeColor = data;
    }
  }
};
</script>

<style lang="less" scoped>
.home_Container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    color: #fff;
    font-size: 20px;
    img {
      width: 50px;
    }
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0px;
  }
  div.toggle-button {
    background-color: #4a5064;
    cursor: pointer;
    font-size: 12px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
</style>